using System;
using System.IO;
using System.Text;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.HighLevel.UI;

using VitaReader;

namespace VitaReaderLibPS
{
	internal class  ReaderConfig
	{
		private String		mPath;
		private String		mFontFile;
		private int			mFontSize;
		private String		mServerAddress;
		private UIColor		mTextColor;
		private UIColor		mBackgroundColor;
		private float		mBackgroundAlpha;
		
		public ReaderConfig(String configPath)
		{
			mPath = configPath;
			if( File.Exists( configPath ) )
			{
				StreamReader reader = new StreamReader(configPath);
				this.FontFile = reader.ReadLine();
				this.FontSize = Convert.ToInt32(reader.ReadLine());
				this.ServerAddress = reader.ReadLine();
				String[] textcolor = reader.ReadLine().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries );
				if( textcolor.Length < 4 )
					this.TextColor = new UIColor(1,1,1,1);
				else
				{
					this.TextColor = new UIColor( Convert.ToSingle(textcolor[0]),
					Convert.ToSingle(textcolor[1]),
					Convert.ToSingle(textcolor[2]),
					Convert.ToSingle(textcolor[3]) );
				}
				
				String[] backcolor = reader.ReadLine().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
				if( backcolor.Length < 4 )
					this.BackgroundColor = new UIColor(1,1,1,1);
				else
				{
					this.BackgroundColor = new UIColor( Convert.ToSingle(backcolor[0]),
					Convert.ToSingle(backcolor[1]),
					Convert.ToSingle(backcolor[2]),
					Convert.ToSingle(backcolor[3]));
				}
				this.BackgroundAlpha = Convert.ToSingle( reader.ReadLine() );
			}
			else
			{
				this.FontFile = "msyh.ttf";
				this.FontSize = 25;
				this.ServerAddress = "";
				this.TextColor = new UIColor(1,1,1,1);
				this.BackgroundColor = new UIColor(0,0,0,0);
				this.BackgroundAlpha = 0.5f;
			}
			
		}
		
		public void SaveConfig()
		{
			StringBuilder sb = new StringBuilder(4096);
			sb.Append(mFontFile);
			sb.Append( '\n' );
			sb.Append( Convert.ToString(mFontSize) );
			sb.Append( '\n' );
			sb.Append(mServerAddress);
			sb.Append('\n');
			sb.Append( String.Format("{0},{1},{2},{3}\n", mTextColor.R, mTextColor.G, mTextColor.B, mTextColor.A ) );
			sb.Append( String.Format("{0},{1},{2},{3}\n", mBackgroundColor.R, mBackgroundColor.G, mBackgroundColor.B, mBackgroundColor.A ) );
			File.WriteAllText(mPath, sb.ToString(), Encoding.Unicode );
		}
		
		public String FontFile
		{
			get {return mFontFile;}
			set {mFontFile = value;}
		}
		
		public int FontSize
		{
			get {return mFontSize;}
			set {mFontSize = value;}
		}
		
		public String ServerAddress
		{
			get {return mServerAddress;}
			set {mServerAddress = value;}
		}
		
		public UIColor TextColor
		{
			get {return mTextColor;}
			set {mTextColor = value;}
		}
		
		public UIColor BackgroundColor
		{
			get {return mBackgroundColor;}
			set {mBackgroundColor = value;}
		}
		
		public float BackgroundAlpha
		{
			get {return mBackgroundAlpha;}
			set {mBackgroundAlpha = value;}
		}
	}//class
}